package com.yhp.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisShopUserMapper;
import com.system.common.pojo.BisShopUser;
import com.system.common.pojo.BisShopUserExample;
import com.system.common.pojo.BisShopUserExample.Criteria;
import com.system.common.pojo.SysUser;

@Service
public class BBisShopUserServiceImpl implements BBisShopUserService {
	@Autowired
	private BisShopUserMapper userMapper;

	/**
	 * 综合查询
	 */
	@Override
	public List<BisShopUser> selectByExample(Map<String, Object> map) {
		String account= (String) map.get("account");
		String shopId= (String) map.get("shopId");
		String postCode= (String) map.get("postCode");
		String status= (String) map.get("status");
		String username= (String) map.get("username");
		String workNumber= (String) map.get("workNumber");
		String mobile= (String) map.get("mobile");
		String idno= (String) map.get("idno");
		BisShopUserExample example=new BisShopUserExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(account)) {
			criteria.andAccountLike("%"+account+"%");
		}
		if (StringUtils.isNotBlank(shopId)) {
			criteria.andShopIdEqualTo(shopId);
		}
		if (StringUtils.isNotBlank(postCode)) {
			criteria.andPostCodeEqualTo(postCode);
		}
		if (StringUtils.isNotBlank(status)) {
			criteria.andStatusEqualTo(status);
		}
		if (StringUtils.isNotBlank(username)) {
			criteria.andUsernameLike("%"+username+"%");
		}
		if (StringUtils.isNotBlank(workNumber)) {
			criteria.andWorkNumberLike("%"+workNumber+"%");
		}
		if (StringUtils.isNotBlank(mobile)) {
			criteria.andMobileLike("%"+mobile+"%");
		}
		if (StringUtils.isNotBlank(idno)) {
			criteria.andIdnoLike("%"+idno+"%");
		}
		List<BisShopUser> selectByExample = this.userMapper.selectByExample(example);
		List<BisShopUser> bisShopUserList=new ArrayList<>();
		for (BisShopUser bisShopUser : selectByExample) {
			List<BisShopUser> select = userMapper.select();
			for (BisShopUser bisShopUser2 : select) {
				if (bisShopUser.getShopUserId().equals(bisShopUser2.getShopUserId())) {
					bisShopUser.setSaleincomes(bisShopUser2.getSaleincomes());
					bisShopUser.setHandincomes(bisShopUser2.getHandincomes());
				}
			}
			if (bisShopUser.getIsDel().equals("0")) {
				bisShopUserList.add(bisShopUser);
			}
		}
		return bisShopUserList;
	}

	/**
	 * 添加
	 */
	@Override
	public int insert(BisShopUser bisShopUser,HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("user");
		// 用户编号 用户类型  是否删除  创建时间  创建用户编号  修改时间  修改用户编号
		Snowflake sf=new Snowflake(0, 0);
		bisShopUser.setShopUserId(sf.nextId()+"");
		bisShopUser.setUserType("2");
		bisShopUser.setIsDel("0");
		bisShopUser.setCreateTime(new Date());
		bisShopUser.setCreateUserId(user.getUserId());
		return userMapper.insert(bisShopUser);
	}

	/**
	 * 修改查询
	 */
	@Override
	public BisShopUser selectByPrimaryKey(String shopUserId) {
		return userMapper.selectByPrimaryKey(shopUserId);
	}

	/**
	 * 修改
	 */
	@Override
	public int updateByPrimaryKey(BisShopUser bisShopUser,HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("user");
		BisShopUser selectByPrimaryKey = userMapper.selectByPrimaryKey(bisShopUser.getShopUserId());
		bisShopUser.setUserType(selectByPrimaryKey.getUserType());
		bisShopUser.setIsDel("0");
		bisShopUser.setCreateTime(selectByPrimaryKey.getCreateTime());
		bisShopUser.setModifyTime(new Date());
		bisShopUser.setCreateUserId(selectByPrimaryKey.getCreateUserId());
		bisShopUser.setModifyUserId(user.getUserId());
		return userMapper.updateByPrimaryKey(bisShopUser);
	}

	/**
	 * 删除
	 */
	@Override
	public int delete(BisShopUser bisShopUser) {
		return userMapper.updateByPrimaryKey(bisShopUser);
	}

	@Override
	public String selectByAccount(String account) {
		BisShopUser selectByAccount = userMapper.selectByAccount(account);
		if (selectByAccount!=null) {
			return "false";
		}
		return "true";
	}

	@Override
	public String selectByWorkNumber(String workNumber) {
		BisShopUser selectByWorkNumber = userMapper.selectByWorkNumber(workNumber);
		if (selectByWorkNumber!=null) {
			return "false";
		}
		return "true";
	}

	@Override
	public int deleteByPrimaryKey(String shopUserId) {
		return userMapper.deleteByPrimaryKey(shopUserId);
	}
}
